package cloudPayAdmin.admin.service.hicatcard.distributor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cloudPayAdmin.admin.dbapp.entity.hicatcard.distributor.TDistributor;
import cloudPayAdmin.admin.dbapp.entity.hicatcard.service.TService;
import cloudPayAdmin.admin.dbapp.repo.hicatcard.distributor.DistributorRepo;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

@Service
@Transactional
public class DistributorService {

    @Autowired
    DistributorRepo distributorRepo;

    @Transactional(readOnly = true)
    public List<TDistributor> findByDistributorNumberAndEnable(String distributorNumber, Boolean enable){
        return distributorRepo.findByDistributorNumberAndEnable(distributorNumber, enable);
    }

    public void saveDistributor(TDistributor distributor){
        distributorRepo.save(distributor);
    }
    
    @Transactional(readOnly = true)
    public List<TDistributor> findAll() {
    	
    	return distributorRepo.findAll(new Specification<TDistributor>() {				
			@Override
			public Predicate toPredicate(Root<TDistributor> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Path<Boolean> enable = root.get("enable");
				query.where(cb.equal(enable, Boolean.TRUE));
				return null;
			}
		});   	
    }
    
    @Transactional(readOnly = true)
    public TDistributor findById(Long distributorId) {
    	return distributorRepo.findOne(distributorId);
    }
}
